home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / tfm / tfm_fontdim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-14  |  6.2 KB  |  195 lines

  1. /* fontdimen.c: handle TFM fontdimens a.k.a. font parameters.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "tfm.h"
  22.  
  23.  
  24. /* This structure maps fontdimen names to numbers (and vice versa).  */
  25. typedef struct
  26. {
  27.   string name;
  28.   unsigned number;
  29. } fontdimen_type;
  30.  
  31. /* The list of fontdimen names and their corresponding numbers.  I wish
  32.    I could figure out a good way to give this list only once (in tfm.h),
  33.    instead of three times.  */
  34.  
  35. static fontdimen_type fontdimen[] = {
  36.   { "slant",            TFM_SLANT_PARAMETER },
  37.   { "space",            TFM_SPACE_PARAMETER },
  38.   { "stretch",            TFM_STRETCH_PARAMETER },
  39.   { "shrink",            TFM_SHRINK_PARAMETER },
  40.   { "xheight",            TFM_XHEIGHT_PARAMETER },
  41.   { "quad",            TFM_QUAD_PARAMETER },
  42.   { "extraspace",        TFM_EXTRASPACE_PARAMETER },
  43.   { "num1",            TFM_NUM1_PARAMETER },
  44.   { "num2",            TFM_NUM2_PARAMETER },
  45.   { "num3",            TFM_NUM3_PARAMETER },
  46.   { "denom1",            TFM_DENOM1_PARAMETER },
  47.   { "denom2",            TFM_DENOM2_PARAMETER },
  48.   { "sup1",            TFM_SUP1_PARAMETER },
  49.   { "sup2",            TFM_SUP2_PARAMETER },
  50.   { "sup3",            TFM_SUP3_PARAMETER },
  51.   { "sub1",            TFM_SUB1_PARAMETER },
  52.   { "sub2",            TFM_SUB2_PARAMETER },
  53.   { "supdrop",            TFM_SUPDROP_PARAMETER },
  54.   { "subdrop",            TFM_SUBDROP_PARAMETER },
  55.   { "delim1",            TFM_DELIM1_PARAMETER },
  56.   { "delim2",            TFM_DELIM2_PARAMETER },
  57.   { "axisheight",        TFM_AXISHEIGHT_PARAMETER },
  58.   { "defaultrulethickness",    TFM_DEFAULTRULETHICKNESS_PARAMETER },
  59.   { "bigopspacing1",        TFM_BIGOPSPACING1_PARAMETER },
  60.   { "bigopspacing2",        TFM_BIGOPSPACING2_PARAMETER },
  61.   { "bigopspacing3",        TFM_BIGOPSPACING3_PARAMETER },
  62.   { "bigopspacing4",        TFM_BIGOPSPACING4_PARAMETER },
  63.   { "bigopspacing5",        TFM_BIGOPSPACING5_PARAMETER },
  64.   { "leadingheight",        TFM_LEADINGHEIGHT_PARAMETER },
  65.   { "leadingdepth",        TFM_LEADINGDEPTH_PARAMETER },
  66.   { "fontsize",            TFM_FONTSIZE_PARAMETER },
  67.   { "version",            TFM_VERSION_PARAMETER },
  68. };
  69.  
  70. /* How many entries we have in the `fontdimen' array.  */
  71. static unsigned fontdimen_array_size
  72.   = sizeof (fontdimen) / sizeof (fontdimen_type);
  73.  
  74. /* Set the font parameter entries in TFM_INFO according to the string S.
  75.    If S is non-null and non-empty, it should look like
  76.    <fontdimen>:<real>,<fontdimen>:<real>,..., where each <fontdimen> is
  77.    either a standard name (in the list above) or a number between 1 and
  78.    TFM_MAX_FONTDIMENS (which is currently 30, the default value in
  79.    PLtoTF).  */
  80.    
  81. void
  82. tfm_set_fontdimens (string s, tfm_global_info_type *tfm_info)
  83. {
  84.   string spec;
  85.   
  86.   /* If S is empty, we have nothing more to do.  */
  87.   if (s == NULL || *s == 0)
  88.     return;
  89.   
  90.   /* Parse the specification string.  */
  91.   for (spec = strtok (s, ","); spec != NULL; spec = strtok (NULL, ","))
  92.     {
  93.       string fontdimen;
  94.       real value;
  95.       unsigned param_number = 0;
  96.       string value_string = strchr (spec, ':');
  97.       
  98.       if (value_string == NULL || !float_ok (value_string))
  99.         FATAL1 ("Fontdimens look like `<fontdimen>:<real>', not `%s'",
  100.                 spec);
  101.  
  102.       value = atof (value_string + 1);
  103.       fontdimen = substring (spec, 0, value_string - spec - 1);
  104.       
  105.       /* If `fontdimen' is all numerals, we'll take it to be an integer.  */
  106.       if (strspn (fontdimen, "0123456789") == strlen (fontdimen))
  107.         {
  108.           param_number = atou (fontdimen);
  109.  
  110.           if (param_number == 0 || param_number > TFM_MAX_FONTDIMENS)
  111.             FATAL2 ("Font parameter %u is not between 1 and the maximum (%u)",
  112.                     param_number, TFM_MAX_FONTDIMENS);
  113.         }
  114.       else
  115.         { /* It wasn't a number; see if it's a valid name.  */
  116.       param_number = tfm_fontdimen_number (fontdimen);
  117.  
  118.       if (param_number == 0)
  119.             FATAL1 ("I don't know the font parameter name `%s'", fontdimen);
  120.         }
  121.       
  122.       /* If we got here, `param_number' is the number of the font
  123.          parameter we are supposed to assign to.  */
  124.       tfm_set_fontdimen (tfm_info, param_number, value);
  125.     }
  126. }
  127.  
  128. /* Return zero if we do not recognize S as the name of a fontdimen, else
  129.    its corresponding number.  We just do a linear search through the
  130.    structure, since it's so small.  */
  131.  
  132. unsigned
  133. tfm_fontdimen_number (string s)
  134. {
  135.   unsigned i; /* Index into the `fontdimen' array.  */
  136.   unsigned param_number = 0;
  137.  
  138.   for (i = 0; i < fontdimen_array_size && param_number == 0; i++)
  139.     if (STREQ (s, fontdimen[i].name))
  140.       param_number = fontdimen[i].number;
  141.  
  142.   return param_number;
  143. }
  144.  
  145.  
  146. /* Return the name in `fontdimens' corresponding to the number N.  */
  147.  
  148. string
  149. tfm_fontdimen_name (unsigned n)
  150. {
  151.   unsigned i;
  152.   string name = NULL;
  153.   
  154.   for (i = 0; i < fontdimen_array_size && name == NULL; i++)
  155.     {
  156.       if (fontdimen[i].number == n)
  157.         name = fontdimen[i].name;
  158.     }
  159.   
  160.   return name;
  161. }
  162.  
  163. /* Set the PARAMETER-th font parameter of TFM_INFO to VALUE.  If
  164.    PARAMETER is beyond the current last parameter of TFM_INFO, set
  165.    all the intervening parameters to zero.  */
  166.  
  167. void
  168. tfm_set_fontdimen (tfm_global_info_type *tfm_info,
  169.                    unsigned parameter, real value)
  170. {
  171.   if (TFM_FONTDIMEN_COUNT (*tfm_info) < parameter)
  172.     {
  173.       unsigned p;
  174.       
  175.       for (p = TFM_FONTDIMEN_COUNT (*tfm_info) + 1; p < parameter; p++)
  176.         TFM_FONTDIMEN (*tfm_info, p) = 0.0;
  177.       
  178.       /* Now we have more parameters.  */
  179.       TFM_FONTDIMEN_COUNT (*tfm_info) = parameter;
  180.     }
  181.  
  182.   TFM_FONTDIMEN (*tfm_info, parameter) = value;
  183. }
  184.  
  185.  
  186. /* Set the `FONTSIZE' parameter of TFM_INFO, if the design size is set.  */
  187.  
  188. void
  189. tfm_set_fontsize (tfm_global_info_type *tfm_info)
  190. {
  191.   if (TFM_DESIGN_SIZE (*tfm_info) != 0.0)
  192.     tfm_set_fontdimen (tfm_info,
  193.                        TFM_FONTSIZE_PARAMETER, TFM_DESIGN_SIZE (*tfm_info));
  194. }
  195.